﻿using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace client
{
    class Program
    {
        static void Main(string[] args)
        {
            doget();
            Console.ReadLine();
        }

        static async void doget()
        {
            //Task<string> integerTask = HttpClientPoxy.Client.GetInstance().GetByBasicAuth("http://localhost:8089/api/books", "jac", "jac", true, false);
            //Task<string> integerTask = HttpClientPoxy.Client.GetInstance().Get("http://localhost:8089/api/books", "", false);
            //string result = await integerTask;

            var gizmo = new Book() { Title = "Gizmo33333", Price = 300, Genre = "Widget", ReleaseDate = DateTime.Now, staute = em_staute.好, author = new Author() { Name = "jacob" } };
            var json = Newtonsoft.Json.JsonConvert.SerializeObject(gizmo);
            //List<Book> bks = new List<Book>();
            //for (int i = 0; i < 100000; i++)
            //{
            //    bks.Add(new Book() { Title = "Gizmo", Price = 300, Genre = "Widget", ReleaseDate = DateTime.Now, staute = em_staute.好, author = new Author() { Name = "jacob" } });
            //}
            Task<string> integerTask = HttpClientPoxy.Client.GetInstance().Post<Book>("http://localhost:8089/api/books", "", gizmo);

            //Task<string> integerTask = HttpClientPoxy.Client.GetInstance().PostString("http://localhost:8089/api/customers/1/orders/2", "", Newtonsoft.Json.JsonConvert.SerializeObject(gizmo));

            //Task<string> integerTask = HttpClientPoxy.Client.GetInstance().Upload("http://127.0.0.1:8089/api/file/upload", new List<string>() { "d:\\i.icoolpy.com_sha256_cn.zip" });
            //string result = await integerTask;
            //var books = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Book>>(result);
            //var gizmo = new Book() { Title = "Gizmo1", Price = 500, Genre = "Widget1", ReleaseDate = DateTime.Now };
            //Task<string> integerTask = HttpClientPoxy.Client.GetInstance().Put<Book>("http://localhost:8089/api/books/56f3b497b6efb705d80d977a", "", gizmo);
            string result = await integerTask;

            Console.WriteLine(result);

        }

        //static async void go()
        //{
        //    //using (var client = new HttpClient())
        //    //{
        //    //    client.BaseAddress = new Uri("http://localhost:9000/");
        //    //    client.DefaultRequestHeaders.Accept.Clear();
        //    //    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        //    //// // HTTP POST
        //    var gizmo = new Book() { Title = "Gizmo", Price = 100, Genre = "Widget", ReleaseDate = DateTime.Now };
        //    var result = GetResponseData(Newtonsoft.Json.JsonConvert.SerializeObject(gizmo), "http://localhost:9000/api/books", "POST");
        //    //    // HTTP PUT
        //    //    var gizmo1 = new Book() { Title = "Gizmo1", Price = 200, Genre = "Widget1", ReleaseDate=DateTime.Now };
        //    //    var result1 = GetResponseData(Newtonsoft.Json.JsonConvert.SerializeObject(gizmo1), "http://localhost:9000/api/books/553ef559b002b62038d662c2", "PUT");
        //    // HTTP GET ALL
        //    //var result = doGet("http://localhost:9000/api/books", "GET");
        //    //// HTTP GET ONE
        //    //result = doGet("http://localhost:9000/api/books/5541913db002b60b204195e6", "GET");
        //    // HTTP DELETE
        //    //result = doGet("http://localhost:9000/api/books/5541913db002b60b204195e6", "DELETE");
        //    //    HttpResponseMessage response = await client.PostAsJsonAsync("api/books", gizmo);
        //    //    if (response.IsSuccessStatusCode)
        //    //    {
        //    //        // Get the URI of the created resource.
        //    //        Uri gizmoUrl = response.Headers.Location;
        //    //    }
        //    //}

        //    //update
        //        //var result = GetResponseData(Newtonsoft.Json.JsonConvert.SerializeObject(gizmo), "http://localhost:9000/api/books/553ef559b002b62038d662c2");
        //        //var result = doGet("http://localhost:9000/api/books");
        //        Console.WriteLine(result);

        //        Console.Read();
        //}

        /// <summary>
        /// 返回JSon数据
        /// </summary>
        /// <param name="JSONData">要处理的JSON数据</param>
        /// <param name="Url">要提交的URL</param>
        /// <returns>返回的JSON处理字符串</returns>
        public static string GetResponseData(string JSONData, string Url, string meth)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(JSONData);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = meth;
	        request.ContentLength = bytes.Length;
            request.ContentType = "application/json";
            Stream reqstream = request.GetRequestStream();
            reqstream.Write(bytes, 0, bytes.Length);

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream streamReceive = response.GetResponseStream();
            Encoding encoding = Encoding.UTF8;

            StreamReader streamReader = new StreamReader(streamReceive, encoding);
            string  strResult = streamReader.ReadToEnd();
            streamReceive.Dispose();
            streamReader.Dispose();

            return strResult;
        }

        public static string doGet(string Url, string meth)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = meth;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream streamReceive = response.GetResponseStream();
            StreamReader streamReader = new StreamReader(streamReceive);
            string strResult = streamReader.ReadToEnd();
            streamReceive.Dispose();
            streamReader.Dispose();

            return strResult;
        }

    }

    public class Book
    {
        public string Title { get; set; }
        public DateTime ReleaseDate { get; set; }
        public string Genre { get; set; }
        public double? Price { get; set; }
        public em_staute staute { get; set; }

        public virtual Author author { get; set; }
    }

    public class Author
    {
        public string Name { get; set; }
    }

    public enum em_staute
    {
        普通 = 0, 好 = 1, 非凡 = 2
    }
}
